package com.biz.primus.ms.cart.service.builder;

import com.biz.primus.model.cart.vo.ShopCartBundleVo;
import com.biz.primus.model.cart.vo.ShopCartRespVo;

/**
 * 购物车Builder
 *
 * @author lei
 * @date 2017/01/17
 */
public class ShopCartBuilder {
    private ShopCartBuilder() {
    }

    private ShopCartRespVo shopCartVo;

    public static ShopCartBuilder createBuilder() {
        ShopCartBuilder builder = new ShopCartBuilder();
        builder.shopCartVo = new ShopCartRespVo();
        return builder;
    }

    public ShopCartBuilder addCartBundle(ShopCartBundleVo shopCartBundleVo, int index) {
        if (shopCartBundleVo == null) {
            return this;
        }
        shopCartVo.getBundles().add(index, shopCartBundleVo);
        this.calculateCartNumAndPrice(shopCartBundleVo);
        return this;
    }

    private void calculateCartNumAndPrice(ShopCartBundleVo shopCartBundleVo) {
        shopCartVo.setCartNum(shopCartVo.getCartNum() + shopCartBundleVo.getTotalAmount());
        shopCartVo.setSelectedNum(shopCartVo.getSelectedNum() + shopCartBundleVo.getSelectedAmount());
        shopCartVo.setTotalPrice(shopCartVo.getTotalPrice() + shopCartBundleVo.getTotalPrice());
    }


    public ShopCartRespVo build() {
        return shopCartVo;
    }
}
